#region License

/*
 Copyright 2014 - 2015 Nikita Bernthaler
 FileLogger.cs is part of SFXLibrary.

 SFXLibrary is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 SFXLibrary is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with SFXLibrary. If not, see <http://www.gnu.org/licenses/>.
*/

#endregion License

#region

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Web.Caching;
using LeagueSharp;
using SFXUtility.Library.Extensions.NET;

#endregion

namespace SFXUtility.Library.Logger
{
    public class FileLogger : ProducerConsumer<LogItem>, ILogger
    {
        private readonly string _fileName;
        private readonly HashSet<string> _unique = new HashSet<string>();

        public FileLogger(string logDir,
            string fileName = "{0}_{1}_{2}.txt",
            int minConsumers = 1,
            int maxConsumers = 3,
            int producersPerConsumer = 5,
            int checkInterval = 10000) : base(minConsumers, maxConsumers, producersPerConsumer, checkInterval)
        {
            LogDir = logDir;
            _fileName = fileName;
            try
            {
                Directory.CreateDirectory(LogDir);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        public Dictionary<string, string> AdditionalData { get; set; }
        public bool Compression { get; set; }
        public bool OutputConsole { get; set; }
        public string LogDir { get; set; }
        public LogLevel LogLevel { get; set; }

        public new void AddItem(LogItem item)
        {
            if (LogLevel == LogLevel.None || item == null || string.IsNullOrWhiteSpace(item.Exception.ToString()))
            {
                return;
            }

            try
            {
                var uniqueValue = (item.Exception + AdditionalData.ToDebugString()).Trim();
                if (!_unique.Contains(uniqueValue))
                {
                    OnItemAdded.RaiseEvent(item, new EventArgs());
                    _unique.Add(uniqueValue);
                    base.AddItem(item);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        public event EventHandler OnItemAdded;

        private void AddData(Exception ex)
        {
            if (ex != null)
            {
                if (GameObjects.Heroes != null && GameObjects.Heroes.Any())
                {
                    ex.Data.Add("Champion", ObjectManager.Player.ChampionName);
                    ex.Data.Add(
                        "Champions", string.Join(", ", GameObjects.Heroes.Select(e => e.ChampionName).ToArray()));
                }
                ex.Data.Add("Version", "6.12");
                ex.Data.Add("Region", Game.Region);
                ex.Data.Add("MapId", Game.MapId);
                ex.Data.Add("Type", Game.Type);
            }
        }

        protected override void ProcessItem(LogItem item)
        {
            if (item == null || string.IsNullOrWhiteSpace(item.Exception.ToString()))
            {
                return;
            }

            try
            {
                var file = Path.Combine(
                    LogDir,
                    string.Format(
                        _fileName, DateTime.Now.ToString("yyyy_MM_dd"), LogLevel.ToString().ToLower(),
                        (item.Exception + AdditionalData.ToDebugString()).ToMd5Hash()));

                if (File.Exists(file))
                {
                    return;
                }

                AddData(item.Exception);

                if (OutputConsole)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(item.Exception);
                    Console.ResetColor();
                }

                using (
                    var fileStream = new FileStream(
                        file, FileMode.CreateNew, FileAccess.Write, FileShare.None, 4096, true))
                {
                    using (Stream gzStream = new GZipStream(fileStream, CompressionMode.Compress, false))
                    {
                        var text = item.Exception.ToString();
                        text =
                            item.Exception.Data.Cast<DictionaryEntry>()
                                .Aggregate(
                                    text + Environment.NewLine + Environment.NewLine,
                                    (current, entry) =>
                                        current +
                                        string.Format("{0}: {1}", entry.Key, entry.Value + Environment.NewLine));

                        if (string.IsNullOrWhiteSpace(text.Trim()))
                        {
                            return;
                        }

                        var logByte = new UTF8Encoding(true).GetBytes(text);

                        if (Compression)
                        {
                            gzStream.Write(logByte, 0, logByte.Length);
                        }
                        else
                        {
                            fileStream.Write(logByte, 0, logByte.Length);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}